app.controller('roleController', ['$scope','ajax','$state', function ($scope,ajax,$state) {

    //初始化页面
    ajax.get({
        url: 'back/mRole/page',
        data:{
            state:1,
            currentPage:1,
            pageSize:10
        }
    }).then(function (result) {

        $scope.role=result.resultData;
    })


    $scope.role={
        roleId:'',
        roleName:'',
        remarks:''
    }

    $scope.roleDetail={
        roleName:'',
        remarks:'',
    }

    //新增管理员
    $scope.addRole=function(){
        $scope.showRole=true;
        $scope.showAddRole=true;
    }



    //提交
    $scope.submitRole=function(){
        console.log($scope);
        if($scope.roleDetail.ctime){
            ajax.post({
                url: 'back/mRole/update',
                data:$scope.roleDetail
            }).then(function (result) {

                $state.go('admin.role',null,{
                    reload:true
                });
            })
            return
        }
        ajax.post({
            url: 'back/mRole/add',
            data:$scope.roleDetail
        }).then(function (result) {

            $state.go('admin.role',null,{
                reload:true
            });
        })
    }


    //编辑管理员
    $scope.updateRole=function(roleId){
        $scope.showRole=true;
        $scope.showAddRole=true;
        ajax.get({
            url: 'back/mRole/query',
            data:{
                roleId:roleId
            }
        }).then(function (result) {
            $scope.roleDetail=result;
        })
    }

    //删除角色
    $scope.deleteRole=function(roleId){
        ajax.post({
            url: 'back/mRole/delete',
            data:{
                roleId:roleId
            }
        }).then(function (result) {

            $state.go('admin.role',null,{
                reload:true
            });
        })

    }

    //权限管理
    $scope.Management=function(roleId){
        $scope.roleId=roleId;
        $scope.showRole=true;
        $scope.showAddRole=false;
        ajax.get({
            url: 'back/mRole/queryResourceTreeByRoleId',
            data:{
                roleId:roleId
            }
        }).then(function (result) {

            $scope.isShowManagement=true;
            $scope.ManagementLevel=result.childTree;
            $scope.tree=result;
        })

    }


    //保存权限管理
    $scope.saveManagement=function(){
        traverse($scope.tree, 0);//遍历树
        ajax.post({
            url: 'back/mRole/updateMRoleResource',
            data:{
                roleId:$scope.roleId,
                resourceCodes:resourceCodes
            }
        }).then(function (result) {
            $scope.isShowManagement=true;
            $state.go('admin.role',null,{
                reload:true
            });
        })
    }

    var resourceCodes=[];
    function traverse(node, i) {

        var children = node.childTree;
        if (children.length!==0) {
            //console.log("parent:" + node.name + ",child:" + children[i].name);
            //判断子节点有没有isCheck==1
            if(children[i].isCheck==1){
                if(resourceCodes.indexOf(children[i].code)==-1 && resourceCodes.indexOf(node.code==-1)){
                    resourceCodes.push(children[i].code);
                }
            }

            if (i == children.length - 1) {
                var k=[];
                if(k.length==0){
                    for(var j=0;j<=i;j++){
                        k.push(j);
                    }
                }
                for(var m=0;m< k.length;m++){
                    traverse(children[m], 0);
                }

            } else {
                traverse(node, i + 1);
            }
        }
    }

    //点击权限按钮
    $scope.isCheckedItem=function(isCheck,data,code){
        //console.log($scope.ManagementLevel)
        if(data.isCheck==0){
            data.isCheck=1;
            //codeArr1.shift(code);

        }
        else{
            data.isCheck=0;
        }
        for(var i= 0,len=data.childTree.length;i<len;i++){
            if(data.childTree[i].isCheck==0){
                data.childTree[i].isCheck=1;

                //var data2=data.childTree[i].childTree;
            }
            else if(data.childTree[i].isCheck==1){
                data.childTree[i].isCheck=0;
                //codeArr2.shift(code);
            }
        }
    }

}]);